package com.sls.web.manager.game.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GamePriceTemplate;
import com.sls.web.manager.game.pojo.GameSpecialPrice;
import com.sls.web.manager.game.pojo.GoodsManageInfo;
import com.sls.web.manager.game.pojo.GoodsSupplyManageInfo;
import com.sls.web.manager.game.querybean.GameSpecialPriceQuery;
import com.sls.web.manager.game.service.GameSpecialPriceService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 特殊价格Impl实现类
 * 
 * @author gesx
 * 
 */
@Repository("gamePriceService")
public class GameSpecialPriceServiceImpl extends SuperService implements
		GameSpecialPriceService {
	private static Logger log = Logger
			.getLogger(GameSpecialPriceServiceImpl.class);
	private Gson gson;

	public GameSpecialPriceServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean saveGameSpecialPrice(GameSpecialPrice gameSpecialPrice,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户："
				+ userInfoVO.getUserId()
				+ "，Enter GameSpecialPriceServiceImpl saveGameSpecialPrice method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SPECIAL_PRICE_SAVE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gameSpecialPrice.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gameSpecialPrice);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			log.info("当前用户："
					+ userInfoVO.getUserId()
					+ "，Leave GameSpecialPriceServiceImpl saveGameSpecialPrice method...");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public boolean updateGameSpecialPrice(GameSpecialPrice gameSpecialPrice,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户："
				+ userInfoVO.getUserId()
				+ "，Enter GameSpecialPriceServiceImpl updateGameSpecialPrice method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SPECIAL_PRICE_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gameSpecialPrice.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gameSpecialPrice);
		log.info("当前用户："
				+ userInfoVO.getUserId()
				+ "，Leave GameSpecialPriceServiceImpl updateGameSpecialPrice method...");
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean deleteGameSpecialPrice(String id, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户："
				+ userInfoVO.getUserId()
				+ ",Enter GameSpecialPriceServiceImpl deleteGameSpecialPrice method.....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SPECIAL_PRICE_DELETE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, id);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器响应信息为：" + dto.getBody());
			return true;

		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<GameSpecialPrice> queryList(
			GameSpecialPriceQuery gameSpecialPriceQuery, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GameSpecialPriceServiceImpl queryList method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SPECIAL_PRICE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, gameSpecialPriceQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<GameSpecialPrice> gamePrice = gson.fromJson(body,
					new TypeToken<Pagination<GameSpecialPrice>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GameSpecialPriceServiceImpl queryList method...");
			return gamePrice;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public GameSpecialPrice queryById(String goodsId, String templateCode,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GameSpecialPriceImpl queryById method......");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SPECIAL_PRICE_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GameSpecialPrice game = new GameSpecialPrice();
		game.setGoodsId(goodsId);
		game.setTemplateCode(templateCode);
		SocketDTO dto = super.send(head, game);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息为：" + dto.getBody());
			GameSpecialPrice game2 = gson.fromJson(dto.getBody(),
					GameSpecialPrice.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GameSpecialPriceServiceImpl queryById method...");
			return game2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<String> queryTemplateNameList(
			GamePriceTemplate gamePriceTemplate, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户："
				+ userInfoVO.getUserId()
				+ "，Enter GameSpecialPriceServiceImpl queryTemplateNameList method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_TEMPLATE_NAME);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, gamePriceTemplate);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			List<String> game = gson.fromJson(body,
					new TypeToken<List<String>>() {
					}.getType());

			log.info("当前用户："
					+ userInfoVO.getUserId()
					+ "，Leave GameSpecialPriceServiceImpl queryTemplateNameList method...");
			return game;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<GoodsManageInfo> queryNotInSpecialPrice(String templateName,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户："
				+ userInfoVO.getUserId()
				+ "，Enter GameSpecialPriceServiceImpl queryTemplateNameList method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_GOODS_MANAGEINFO_GOODSID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GamePriceTemplate game = new GamePriceTemplate();
		game.setTemplateName(templateName);
		SocketDTO dto = super.send(head, game);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			List<GoodsManageInfo> goods = gson.fromJson(body,
					new TypeToken<List<GoodsManageInfo>>() {
					}.getType());

			log.info("当前用户："
					+ userInfoVO.getUserId()
					+ "，Leave GameSpecialPriceServiceImpl queryTemplateNameList method...");
			return goods;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public String queryHighInPrice(String goodsId, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GameSpecialPriceServiceImpl queryList method...");
		GoodsSupplyManageInfo info = new GoodsSupplyManageInfo();
		info.setGoodsId(goodsId);

		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_GOODS_HIGHINPRICE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			String goods2 = gson.fromJson(body, String.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GameSpecialPriceServiceImpl queryList method...");
			return goods2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean checkGoodsName(String goodsName, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.CHECK_GAME_SPECIAL_PRICE_GOODSNAME);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, goodsName);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			String result = gson.fromJson(body, String.class);
			if ("true".equals(result)) {
				return true;
			} else {
				return false;
			}
		} else {

			return false;
		}
	}

	public List<GameSpecialPrice> querySpecialPriceInfo(String templateName,
			String brandName, UserInfoVO userInfoVO) throws Exception {
		GameSpecialPriceQuery query = new GameSpecialPriceQuery();
		query.setCurrentPage(1);
		query.setPageSize(20);
		query.setTemplateName(templateName);
		query.setBrandName(brandName);

		List<GameSpecialPrice> spList = new LinkedList<GameSpecialPrice>();
		Pagination<GameSpecialPrice> page = queryList(query, userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				spList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (query.getPageSize() == 0) {
				query.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
				query.setCurrentPage(i + 1);
				page = queryList(query, userInfoVO);
				if (page.getList() != null) {
					spList.addAll(page.getList());
				}
			}
			return spList;
		}
		return null;
	}

	@Override
	public String querySpecialPrice(String templateName, String brandName,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.EXPORT_SPECIAL_PRICE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GameSpecialPrice special = new GameSpecialPrice();
		special.setTemplateName(templateName);
		special.setBrandName(brandName);
		SocketDTO dto = super.send(head, special);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			String result = gson.fromJson(body, String.class);
			return result;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<String> queryTemplateNames(UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_TEMPLATE_NAME_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				List<GameSpecialPrice> specialList = gson.fromJson(body,
						new TypeToken<List<GameSpecialPrice>>() {
						}.getType());
				List<String> nameList = new ArrayList<String>();
				if (specialList != null) {
					for (GameSpecialPrice sp : specialList) {
						nameList.add(sp.getTemplateName());
					}
				}

				return nameList;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void copy(GameSpecialPrice gameSpecialPrice, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021028");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gameSpecialPrice.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gameSpecialPrice);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {

		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public List<GameSpecialPrice> queryPriceTemplateInfoList(
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021029");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			List<GameSpecialPrice> list = gson.fromJson(body,
					new TypeToken<List<GameSpecialPrice>>() {
					}.getType());
			return list;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}
}
